<template>
    <rx-dialog @handOk="handleSubmit" @cancel="cancel">
      <a-form :form="form">
        <a-form-item style="display:none">
          <a-input v-decorator="['defId']"/>
        </a-form-item>
        <a-row>
          <a-col :span="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="i18n('form.sourceTitle','源标题')">
              {{mdl.oldName}}
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="i18n('form.sourceCode','源编码')">
              {{mdl.oldKey}}
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="i18n('form.newTitle','新标题')">
                <rx-input-i18n :placeholder="i18n('form.newTitle','新标题','input')" :isDecorator="true"
                               v-decorator="['name', {rules: [{required: true, message: i18n('form.newTitle','新标题','input')}]}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="i18n('form.newCode','新编码')">
              <a-input :placeholder="i18n('form.newCode','新编码','input')" @change="changeKey" v-decorator="['key', {rules: [{required: true, message: i18n('form.newCode','新编码','input')}]}]"/>
              <span v-if="!isValid" class="errMsg" >{{errorMsg}}</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('form.publishOrNot','是否发布')">
              <a-checkbox @change="isPublicChange" :checked="isPublic" v-decorator="['deploy']">{{$ti18('rxList.deploy','发布')}}</a-checkbox>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </rx-dialog>
</template>

<script>
    import BpmDefApi from "@/api/bpm/core/bpmDef";
    import {BaseForm, RxDialog,RxInputI18n} from '@lowcode/jpaas-common-lib';
    export default {
      name: "BpmDefCopy",
      mixins: [BaseForm],
      components: {
        RxDialog,
          RxInputI18n
      },
      data(){
        return {
          isPublic:false,
          errorMsg:"",
          isValid:false
        }
      },
      methods:{
          i18n(name,text,key){
              return this.$ti18(name,text,"bpmDef",key);
          },
        onload_(values){
          values.deploy=this.isPublic;
          values.oldName=values.name;
          values.oldKey=values.key;
          values.name='';
          values.key='';
        },
        get(id){
          return BpmDefApi.get(id);
        },
        save(values){
          if (!/^[a-zA-Z][a-zA-Z0-9_]*$/g.test(values.key)) {
            notification.error({
              message: this.$ti18('rxMsa.actionTips','操作提示'),
              description: this.i18n('engStart','新编码要求英文+数字,英文开头')
            });
            return;
          }
          return BpmDefApi.copyDef(values);
        },
        isPublicChange(e) {
          this.isPublic=e.target.checked;
        },
        changeKey(e){
          if (!/^[a-zA-Z][a-zA-Z0-9_]*$/g.test(e.target.value)) {
            this.isValid=false;
            this.errorMsg = this.i18n('engStart','新编码要求英文+数字,英文开头');
          }else {
            this.isValid=true;
          }
        }
      }
    }
</script>

<style scoped>
  .errMsg{
    color: red;
    font-size: 12px;
    font-weight: bold;
  }
</style>